#!/system/bin/sh
# Donated code that was put under PD license.
#
# Stripped PRNGd out of it for the time being.

umask 022

CAT=/system/bin/cat
KILL=/system/bin/kill

: ${MODDIR:="$(realpath "$(dirname "$0")")"}
export MODDIR
prefix="$MODDIR/usr"
sysconfdir=/data/ssh
piddir=/data/ssh

SSHD=$prefix/bin/sshd
PIDFILE=$piddir/sshd.pid


SSH_KEYGEN=$prefix/bin/ssh-keygen

stop_service() {
    if [  -r $PIDFILE  -a  ! -z ${PIDFILE}  ]; then
	PID=`${CAT} ${PIDFILE}`
    fi
    if [  ${PID:=0} -gt 1 -a  ! "X$PID" = "X "  ]; then
	${KILL} ${PID}
    else
	echo "Unable to read PID file"
    fi
}

start_service() {
    # XXX We really should check if the service is already going, but
    # XXX we will opt out at this time. - Bal

    # Check to see if we have keys that need to be made
    ${SSH_KEYGEN} -A

    # Start SSHD
    echo "starting $SSHD... \c"         ; $SSHD

    sshd_rc=$?
    if [ $sshd_rc -ne 0 ]; then
	echo "$0: Error ${sshd_rc} starting ${SSHD}... bailing."
	exit $sshd_rc
    fi
    echo done.
}

case $1 in

'start')
    start_service
    ;;

'stop')
    stop_service
    ;;

'restart')
    stop_service
    start_service
    ;;

*)
    echo "$0:  usage:  $0 {start|stop|restart}"
    ;;
esac
